﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using EQT.Dal;
using System.Threading;
using EQT.Model;
using System.Collections.Generic;
using Brettle.Web.NeatUpload;

public partial class Notice : System.Web.UI.Page
{
    private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private DaNotice dalNotice = new DaNotice();
    private DaTfile dalTfile = new DaTfile();
    private UserContext context;

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;

        context = Session["USER_CONTEXT"] as UserContext;
        OperationResult.Text = "";
        divResult.Visible = false;
        AspNetPager.PageSize = 10;

        if (!IsPostBack)
        {
            //FCKeditorContent.FormatOutput = true;
            //FCKeditorContent.FormatSource = true;

            this.QueryNotice();

            if (Request.QueryString["noticeid"] != null && !string.IsNullOrEmpty(Request.QueryString["noticeid"].ToString()))
            {
                this.DisplayDetailView(Request.QueryString["noticeid"].ToString());
            }
            //TODO:检查是对公消息还是定向 20090823 公告：DOC_ACCESS_NOTICE
            //if (Request.QueryString["type"] = "doc_access_notice")
            //{
                
            //}
            

        }
    }

    private void QueryNotice()
    {
        int pagecount = 0;
        string strCondition = "where 1=1 ";

        if (context.RoleID != "Administrators")
        {
            strCondition = strCondition + " and uid='" + context.UserID + "'";
        }

        this.Repeater.DataSource = dalNotice.GetNoticeRecords(strCondition, AspNetPager.PageSize, AspNetPager.CurrentPageIndex - 1, out pagecount);

        Repeater.DataBind();

        AspNetPager.RecordCount = pagecount;
    }

    //protected void btnUpLoad_Click(object sender, EventArgs e)
    //{
    //    string url = String.Format("~/uploadfiles.aspx?notice&id={0}&m=0", ViewState["NOTICE_ID"].ToString());
    //    Response.Redirect(url, false);
    //}

    protected bool DeleteInfo(string strKey)
    {
        //执行删除操作。
        return dalNotice.DeleteNotice(strKey);
    }

    protected void AspNetPager_PageChanged(object sender, EventArgs e)
    {
        this.QueryNotice();
    }

    protected void Repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "delete")
        {
            string strKey = e.CommandArgument.ToString();
            try
            {
                this.DeleteInfo(strKey);
                Utility.DeleteNoticeContent(strKey);

                this.QueryNotice();
                OperationResult.Text = "删除消息成功!"; divResult.Visible = true;
            }
            catch (Exception ex)
            {
                log.Error(ex);
                OperationResult.Text = "删除消息失败!"; divResult.Visible = true;
            }
        }
        else
        {
            //detail
            string strKey = e.CommandArgument.ToString();
            DisplayDetailView(strKey);

        }
        //else
        //{ 
        //    //edit
        //    string strKey = e.CommandArgument.ToString();
        //    DisplayEditView(strKey);
        //}
    }

    private void DisplayDetailView(string noticeid)
    {
        DataSet mod = dalNotice.GetNoticeRecords(" where notice_id='" + noticeid + "'");
        labCreate_Time.Text = mod.Tables[0].Rows[0]["Create_Time"].ToString();
        labDisplay_Name.Text = mod.Tables[0].Rows[0]["Display_Name"].ToString();
        labNotice_ID.Text = noticeid;
        labPublish_Type_Name.Text = mod.Tables[0].Rows[0]["Publish_Type_Name"].ToString();
        labTitle.Text = mod.Tables[0].Rows[0]["Title"].ToString();
        noticeframe.Attributes.Remove("src");
        noticeframe.Attributes.Add("src", "NoticeFile\\" + noticeid + ".htm");
        RepFile.DataSource = dalTfile.QueryFileRelation(noticeid);
        RepFile.DataBind();

        this.SetPanelVisible(DetailPanel);
    }

    //private void DisplayEditView(string noticeid)
    //{
    //    MoNotice mod = dalNotice.GetEntity(noticeid);
    //    if (mod != null)
    //    {
    //        labType.Text = mod.Publish_Type == 0 ? "公告消息" : "定向消息";
    //        user.Visible = mod.Publish_Type == 0 ? false : true;
    //        CreateNotice_Title.Text = mod.Title;
    //        labNoticeID.Text = mod.Notice_Id;

    //        if (mod.Publish_Type == 1)
    //        {
    //            DaTuser dal = new DaTuser();
    //            CheckUsers.DataSource = dal.Query(" where status=2");
    //            CheckUsers.DataBind();

    //            DaNoticeReceiver darec = new DaNoticeReceiver();
    //            IList<MoNoticeReceiver> lists = darec.GetEntities(" where notice_id='" + mod.Notice_Id + "'");
    //            if (lists != null)
    //            {
    //                foreach (MoNoticeReceiver modr in lists)
    //                {
    //                    ListItem item = CheckUsers.Items.FindByValue(modr.Uid);
    //                    if (item != null)
    //                    {
    //                        item.Selected = true;
    //                    }
    //                }
    //            }
    //        }
    //        string filename = "NoticeFile//" + mod.Notice_Id + ".htm";

    //        filename = Server.MapPath(filename);
    //        if (!string.IsNullOrEmpty(filename))
    //        {
    //            FCKeditorContent.Value = System.IO.File.ReadAllText(filename);
    //        }

    //        RepFileEdit.DataSource = dalTfile.QueryFileRelation(noticeid);
    //        RepFileEdit.DataBind();

    //        this.SetPanelVisible(EditPanel);
    //    }
    //}

    protected void RepFile_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string strKey = e.CommandName.ToString();

        try
        {
            dalTfile.DeleteOneAttachFile(strKey);
            //this.QueryNotice();
            this.DisplayDetailView(labNotice_ID.Text);
            OperationResult.Text = "删除附件成功!"; divResult.Visible = true;
        }
        catch (Exception ex)
        {
            log.Error(ex);
            OperationResult.Text = "删除附件失败!"; divResult.Visible = true;
        }
    }

    protected void btnDetailReturn_Click(object sender, EventArgs e)
    {
        this.SetPanelVisible(SummaryPanel);
    }

    protected void btnAttachFile_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(labNotice_ID.Text))
        {
            Response.Redirect("uploadfiles.aspx?notice&id=" + labNotice_ID.Text + "&m=0");
        }
    }

    //protected void EditSave_Click(object sender, EventArgs e)
    //{
    //    if (CreateNotice_Title.Text.Trim() == "")
    //    {
    //        OperationResult.Text = "请输入消息题目!"; divResult.Visible = true;
    //        return;
    //    }

    //    MoNotice mod = dalNotice.GetEntity(labNoticeID.Text);

    //    try
    //    {
    //        mod.Create_Time = DateTime.Now;
    //        mod.Title = CreateNotice_Title.Text.Trim();

    //        List<string> users = new List<string>();

    //        if (mod.Publish_Type == 0)
    //        {
    //            DaTuser daluser = new DaTuser();
    //            DataSet ds = daluser.GetAllAvailableUsers();
    //            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    //            {
    //                users.Add(ds.Tables[0].Rows[i]["UID"].ToString());
    //            }
    //        }
    //        else
    //        {
    //            for (int i = 0; i < CheckUsers.Items.Count; i++)
    //            {
    //                if (CheckUsers.Items[i].Selected)
    //                {
    //                    users.Add(CheckUsers.Items[i].Value);
    //                }
    //            }

    //            if (users.Count < 1)
    //            {
    //                OperationResult.Text = "请选择消息的接收人！"; divResult.Visible = true;
    //                return;
    //            }
    //        }
    //        if (dalNotice.EditNoticeSave(mod, users))
    //        {
    //            Utility.SaveNoticeContent(mod.Notice_Id, FCKeditorContent.Value);
    //            OperationResult.Text = "修改消息成功!"; divResult.Visible = true;
    //            EditSave.Enabled = false;
    //        }
    //        else
    //        {
    //            OperationResult.Text = "修改消息失败!"; divResult.Visible = true;
    //        }
    //    }
    //    catch (Exception ex)
    //    {
    //        log.Error(ex);
    //        OperationResult.Text = "修改消息失败!"; divResult.Visible = true;
    //    }
    //}

    public string Substring(string strContent)
    {
        return strContent.Length > 30 ? strContent.Substring(0, 30) + "..." : strContent;
    }

    protected void SetPanelVisible(Panel panel)
    {
        SummaryPanel.Visible = false;
        DetailPanel.Visible = false;
        //EditPanel.Visible = false;

        panel.Visible = true;
    }
}
